package core.test;

import core.net.DriveRequest;
import core.net.Json;
import core.oauth.ServiceFactory;
import core.pojos.DriveDir;
import core.pojos.DriveItem;
import java.io.File;
import java.io.FileOutputStream;

import java.io.IOException;
import java.io.OutputStream;

import java.net.URISyntaxException;


import java.util.Scanner;


import static org.scribe.model.OAuthConstants.EMPTY_TOKEN;
import org.scribe.model.Token;
import org.scribe.model.Verifier;
import org.scribe.oauth.OAuthService;

/**
 * Hello world!
 *
 */
public class DriveApiExample {

    public static void main(String[] args) throws IOException, URISyntaxException {

        OAuthService service = ServiceFactory.create(ServiceFactory.DRIVE);
        Scanner in = new Scanner(System.in);
        System.out.println("=== Drive's OAuth Workflow ===");
        System.out.println();

        System.out.println("Fetching the Request Token...");
        String authorizationUrl = service.getAuthorizationUrl(EMPTY_TOKEN);
        System.out.println("Got the Authorization URL!");
        System.out.println(authorizationUrl);
        System.out.println("And paste the verifier here");
        System.out.print(">>");
        Verifier verifier = new Verifier(in.nextLine());
        System.out.println();

        System.out.println("Trading the Request Token for an Access Token...");
        Token token = service.getAccessToken(EMPTY_TOKEN, verifier);
        System.out.println("Got the Access Token!");
        System.out.println("(if your curious it looks like this: " + token + " )");
        System.out.println();

        DriveRequest request = new DriveRequest(token);
        System.out.println("");
        System.out.println("Listing content of root directory ...");
        String json = request.list("");
        // deserialize json string to list DropBoxItem
        DriveDir dir = Json.ToDriveDir(json);
        for (DriveItem item : dir.items) {
            System.out.println(item.title + " " + item.mimeType + " " + item.id);
        }

//        System.out.println("Listing content of first directory...");
//        for (DriveItem folder : dir.getFolder()) {
//            json = request.list(folder.id);
//            // deserialize json string to list DropBoxItem
//            dir = Json.ToDriveList(json);
//            for (DriveItem item : dir.items) {
//                System.out.println(item.title + " " + item.mimeType);
//            }
//            break;
//        }
        
//        System.out.println("\nDownload!!!");
//        String Downloadurl =  request.download("0B_vA8Zj1ofHCWlo3YnVaOXNoQlU");
//        System.out.println("Click the link below to download file : 0B_vA8Zj1ofHCWlo3YnVaOXNoQlU \n" + Downloadurl);
//                System.out.println("");
//        System.out.println("Downloading... file b.txt");
//        File target = new File("src/main/resources/b.txt");
//
//        System.out.println( request.download("0B_vA8Zj1ofHCWlo3YnVaOXNoQlU") );
//
//        System.out.println("Done!!!");
        

        
//        String json2 = request.list("");
//        System.out.println("Uploading... file a.txt");
//        File file = new File("src/main/resources/a.txt");
//        json2 = request.Upload("application/vnd.google-apps.document", "Testing", "docx", file);
//        DriveItem di = Json.ToDriveItemList(json2);
//        System.out.println(" The file "+di.title + " " + di.mimeType+ " just have uploaded!!!!");
        
        
        /////delete
//        request.delete("1CI6ZL4YbL1roMUWQ2PbQTt2uhg-7v0S16TPVABc8_mk");
        
        ////createfolder
//       request.createfolder("royal123", "root");
        
       
       ///rename
//       request.rename("HelloABCDroyal1234", "txt", "17y5VeGIFDzvL7-MkphvqZS9ieEPh2mzA7KVgAnq9xVw");
        
        //movefile
//        request.movefile("0B_vA8Zj1ofHCOUNEXzVpMzhWWVNhSjVIWi0yYy1UOFpSa0NZ","0B_vA8Zj1ofHCY24xWUF2V0JTX00");
//          request.movefile("0B_vA8Zj1ofHCOUNEXzVpMzhWWVNhSjVIWi0yYy1UOFpSa0NZ","root");
       
//        System.out.println("Done!!!");
        
    }

}
